package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.Advice.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private LeaseAgreementService leaseAgreementService;
    @Override
    public void pageItem(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        Page<RoomItemVo> pagedItem = roomInfoMapper.pageItem(page, queryVo);

    }

    @Override
    public void roomSaveOrUpdate(RoomSubmitVo roomSubmitVo) {
        boolean roomUpdate = roomSubmitVo.getId() != null;
        saveOrUpdate(roomSubmitVo);
        if (roomUpdate) {
            LambdaQueryWrapper<RoomAttrValue> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(queryWrapper);//删除属性

            LambdaQueryWrapper<RoomFacility> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(queryWrapper2);//删除配套

            LambdaQueryWrapper<RoomLabel> queryWrapper4 = new LambdaQueryWrapper<>();
            queryWrapper4.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(queryWrapper4);//删除标签

            LambdaQueryWrapper<RoomLeaseTerm> queryWrapper5 = new LambdaQueryWrapper<>();
            queryWrapper5.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            roomLeaseTermService.remove(queryWrapper5);//删除租期

            LambdaQueryWrapper<RoomPaymentType> queryWrapper6 = new LambdaQueryWrapper<>();
            queryWrapper6.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(queryWrapper6);//删除支付方式

            LambdaQueryWrapper<GraphInfo> queryWrapper3 = new LambdaQueryWrapper<>();
            queryWrapper3.eq(GraphInfo::getItemId, roomSubmitVo.getId());
            queryWrapper3.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(queryWrapper3);//删除图片
        }
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();//从 RoomSubmitVo 对象中获取图片信息列表 graphVoList，用于后续保存房间相关的图片信息
        if (graphVoList != null && graphVoList.size() > 0){
            List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());//初始化一个用于存储 GraphInfo 实体对象的列表，并设置初始容量为 graphVoList.size()
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
              graphInfo.setItemId(roomSubmitVo.getId());
              graphInfo.setItemType(ItemType.ROOM);
              graphInfo.setName(graphVo.getName());
              graphInfo.setUrl(graphVo.getUrl());
              graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);//将转换后的房间图片信息列表 graphInfoList 批量保存到数据库中。这是Mybarts中的插入方法
            }

        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();//从 RoomSubmitVo 获取属性值ID列表 attrValueIds
        if(attrValueIds != null && attrValueIds.size() > 0){
            ArrayList<RoomAttrValue> roomAttrValues = new ArrayList<>(attrValueIds.size());
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().attrValueId(attrValueId).roomId(roomSubmitVo.getId()).build();//创建一个 RoomAttrValue 实体对象，并设置属性值ID和房间ID
                roomAttrValues.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValues);//批量保存房间属性值
        }
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();//从 RoomSubmitVo 获取配套信息列表 facilityInfoIds
        if(facilityInfoIds != null && facilityInfoIds.size() > 0){
            ArrayList<RoomFacility> roomFacilities = new ArrayList<>(facilityInfoIds.size());//初始化一个用于存储 RoomFacility 实体对象的列表，并设置初始容量为 facilityInfoIds.size()
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder().facilityId(facilityInfoId).roomId(roomSubmitVo.getId()).build();//创建一个 RoomFacility 实体对象，并设置配套信息ID和房间ID
                roomFacilities.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilities);//批量保存房间配套信息
        }
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if(labelInfoIds != null && labelInfoIds.size() > 0){
            ArrayList<RoomLabel> roomLabels = new ArrayList<>(labelInfoIds.size());
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder().labelId(labelInfoId).roomId(roomSubmitVo.getId()).build();
                roomLabels.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabels);
        }
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if(leaseTermIds != null && leaseTermIds.size() > 0){
            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>(leaseTermIds.size());
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().leaseTermId(leaseTermId).roomId(roomSubmitVo.getId()).build();
                roomLeaseTerms.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if(paymentTypeIds != null && paymentTypeIds.size() > 0){
            ArrayList<RoomPaymentType> roomPaymentTypes = new ArrayList<>(paymentTypeIds.size());
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().paymentTypeId(paymentTypeId).roomId(roomSubmitVo.getId()).build();
                roomPaymentTypes.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypes);
        }
    }

    @Override
    public RoomDetailVo detailById(Long id) {
        RoomDetailVo roomDetailVo = roomInfoMapper.detalId(id);
        return roomDetailVo;
    }

    @Override
    public void roomRemove(Long id) {
        LambdaQueryWrapper<LeaseAgreement> queryWrapper0 = new LambdaQueryWrapper<>();
        queryWrapper0.eq(LeaseAgreement::getRoomId,id);//查询该房间是否有正在使用的租约
        queryWrapper0.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);//筛选出正在使用的租约
        if(leaseAgreementService.count(queryWrapper0) > 0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        LambdaQueryWrapper<RoomAttrValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(queryWrapper);//删除房间属性值
        LambdaQueryWrapper<RoomFacility> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(queryWrapper1);//删除房间配套信息
        LambdaQueryWrapper<RoomLabel> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(queryWrapper2);//删除房间标签
        LambdaQueryWrapper<RoomLeaseTerm> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermService.remove(queryWrapper3);//删除房间租期信息
        LambdaQueryWrapper<RoomPaymentType> queryWrapper4 = new LambdaQueryWrapper<>();
        queryWrapper4.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(queryWrapper4);//删除房间支付方式信息
        LambdaQueryWrapper<GraphInfo> queryWrapper5 = new LambdaQueryWrapper<>();
        queryWrapper5.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(queryWrapper5);//删除房间图片信息

        removeById(id);
    }
}




